<?php
/**
 * Created by PhpStorm.
 * User: sunbo
 * Date: 2019/7/3
 * Time: 6:48 PM
 */
namespace app\index\controller;
use think\Exception;
class Apphome extends CoinBaseContorller
{

    /**
     *
     * @api {post} api/apphome/get_coin  代币信息
     * @apiDescription  代币信息
     * @apiGroup apphome
     * @apiSuccess {string}  name  代币名称
     * @apiSuccess {string}  tokencount  资产
     * @apiSuccess {string}  address 地址
     * @apiSuccess {string}  sum_earnings 总收益
     * @apiSuccess {string}  today_earnings 今日收益
     * @apiSuccess {string}  today_count N天收益
     * @apiVersion 1.0.1
     */
    public  function  get_coin(){
      $userguid=$this->getuserGuid();
      return $this->json_data( $this->_get_coin($userguid),"");
    }

    public function  _get_coin($userguid){
       $coin=$this->db_select("select b.guid,a.address,b.name,0 tokencount,0 sum_earnings,0 today_earnings,0 today_count from token_user_coin_address a inner join token_sys_coin b on a.token_sys_coin_guid=b.guid and b.iscustom=1 WHERE a.token_sys_user_guid='".$userguid."'");
       $coin[0]["tokencount"]=$this->GetCoinCount($coin[0]["guid"],$userguid)/pow(10,8);
       //获取总收益
       $row=$this->db_select("select SUM(earnings_tokencount) tokencount FROM token_bill_earnings WHERE token_sys_user_guid='".$userguid."'  GROUP BY token_sys_user_guid");
       if(count($row)>0){
           $coin[0]["sum_earnings"]=$row[0]["tokencount"]/pow(10,8);
       }
       //获取今日收益
        $row=$this->db_select("select earnings_tokencount tokencount FROM token_bill_earnings WHERE token_sys_user_guid='".$userguid."' and billdate='".date('Y-m-d',strtotime('-1 day'))."' ");
        if(count($row)>0){
            $coin[0]["today_earnings"]=$row[0]["tokencount"]/pow(10,8);
        }

        //获取收益天数
        $row=$this->db_select("select value from token_sys_config where guid='1'");
        $coin[0]["today_count"]=$row[0]["value"];
       return $coin[0];
    }



    /**
     *
     * @api {post} api/apphome/get_coin_list  币列表
     * @apiDescription  币列表
     * @apiGroup apphome
     * @apiSuccess {string}  name  币名
     * @apiSuccess {string}  iscustom  是否代币 1 是 0 否
     * @apiSuccess {string}  ratio  1:N 代币比例
     * @apiSuccess {string}  tokencount  币数量
     * @apiSuccess {string}  cus_tokencount  能兑换的代币数量
     * @apiSuccess {string}  address 地址
     * @apiVersion 1.0.1
     */
    public  function  get_coin_list(){
        $userguid=$this->getuserGuid();
        $data=$this->_get_coin_list($userguid);
        for ($x=0;$x<count($data);$x++){
            $data[$x]["tokencount"]= $data[$x]["tokencount"]/pow(10,8);
            $data[$x]["cus_tokencount"]=  $data[$x]["cus_tokencount"]/pow(10,8);
        }
        return $this->json_data($data,"");
    }

    function  _get_coin_list($userguid){
       $ratios=$this->GetRatio();
       $coinlist=$this->db_select("SELECT a.guid,a.name,a.iscustom,".$ratios["ratio"]." ratio,0 tokencount,0 cus_tokencount,c.address from token_sys_coin a 
        inner join token_user_coin_address c on c.token_sys_coin_guid=a.guid WHERE c.token_sys_user_guid='".$userguid."'");
       //获取btc余额
       $coin=$this->db_select("select guid from token_sys_coin");
        for ($x=0;$x<count($coin);$x++){
            $coinlist=$this->setCoinCount($coinlist,$coin[$x]["guid"],$userguid);
        }
      return $coinlist;
    }

    function  setCoinCount($coinlist,$coinguid,$userguid){
        for ($x=0;$x<count($coinlist);$x++){
            if($coinlist[$x]["guid"]==$coinguid){
                $coinlist[$x]["tokencount"]=$this->GetCoinCount($coinguid,$userguid);
                $cus_tokencount=($coinlist[$x]["tokencount"]* pow(10,8))*($coinlist[$x]["ratio"]* pow(10,8))/ pow(10,16);
                $coinlist[$x]["cus_tokencount"]=round($cus_tokencount,8);
                //如果是代币自己则兑换率就是1
                if($coinguid=="2"){
                    $coinlist[$x]["cus_tokencount"]=$coinlist[$x]["tokencount"];
                    $coinlist[$x]["ratio"]="1";
                }
            }
        }
        return $coinlist;
    }

    /**
     *
     * @api {post} api/apphome/getAPPCoinInfo  获取币信息
     * @apiDescription  获取币种的 数量 金额
     * @apiGroup apphome
     * @apiParam {string} coinguid 币guid
     * @apiSuccess {string}  tokencount  数量
     * @apiSuccess {string}  price  价格
     * @apiVersion 1.0.1
     */
    public function  getAPPCoinInfo(){
        $coinguid=$this->param("coinguid");
        $userid=$this->getuserGuid();
        $info=array();
        $info["tokencount"]=$this->_getCoinInfo($coinguid,$userid)/pow(10,8);
        $info["price"]=0;
        return $this->json_data($info,"");
    }


    function  _getCoinInfo($coinguid,$userid){
        $count=$this->GetCoinCount($coinguid,$userid);
        return $count;
    }

}